import 'package:flutter/material.dart';
import 'package:wallpaper/components/AlertDialog/my_alert_dialog.dart';
import 'package:wallpaper/components/appbar_base.dart';
import 'package:wallpaper/tools/down_image.dart';

class CacheClean extends StatefulWidget {
  const CacheClean({super.key});

  @override
  State<CacheClean> createState() => _CacheCleanState();
}

class _CacheCleanState extends State<CacheClean> {
  String cacheSize = '计算中';
  void getCacheSize() async {
    double size = await DownImage.getTotalCacheSize();
    setState(() {
      // 保留两位小数
      cacheSize = size.toStringAsFixed(2);
    });
  }

  @override
  void initState() {
    getCacheSize();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppbarBase(title: '缓存清理'),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Flex(
          direction: Axis.vertical,
          children: [
            Material(
                color: Theme.of(context).colorScheme.primaryContainer,
                borderRadius: BorderRadius.circular(8),
                clipBehavior: Clip.antiAlias,
                child: ListTile(
                  title: Text('图片缓存'),
                  // 右侧图标
                  trailing: Text('$cacheSize MB'),
                  onTap: () {
                    MyAlertDialog(
                      title: '清理缓存',
                      content: '确定删除所有图片缓存吗？',
                      onPressed: () {
                        DownImage.clearAllCache();
                        setState(() {
                          cacheSize = '0.0';
                        });
                      },
                    ).show(context);
                  },
                ))
          ],
        ),
      ),
    );
  }
}
